package com.yhp.web;

import com.yhp.bean.Menu;
import com.yhp.bean.Users;
import com.yhp.service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/login")
public class loginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("我进来了");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users login = usersService.login(username, password);
        List<Menu> menuList = login.getMenuList();
        System.out.println(menuList==null?"我没有值":"我有值");
        System.out.println(menuList.toString());
        System.out.println(menuList.get(0).getMenuId());
        if(login == null){
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter writer = resp.getWriter();
            writer.println("<script>location.href='login.jsp';alert('账号密码错误');</script>");
        }else{
            req.getSession().setAttribute("username",username);
            req.getSession().setAttribute("name",login);
            resp.sendRedirect("index.jsp");


        }

    }
}
